package com.farm.kaiping.controller;


import com.farm.kaiping.entity.SysRole;
import com.farm.kaiping.po.SysRoleMenuPO;
import com.farm.kaiping.po.SysRolePO;
import com.farm.kaiping.service.SysRoleService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farm.common.util.PageParaUtils;
import com.farm.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统角色表(SysRole)表控制层
 *
 * @author zhuhui
 * @since 2024-11-26 23:18:22
 */
@RestController
@RequestMapping("/sysRole")
@AllArgsConstructor
@Api(tags = "系统角色表控制器")
public class SysRoleController {

	private final SysRoleService sysRoleService;

	@ApiOperation(value = "分页查询系统角色表", notes = "权限：view_sysRole_page")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRole_page')")
	@PostMapping("/page")
	public R<Page<SysRole>> getSysRolePage(@RequestBody SysRolePO sysRolePO) {
		try{
		Page page = PageParaUtils.withdrawPageByParamType(sysRolePO);
		return R.ok(sysRoleService.getSysRolePage(page, sysRolePO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询系统角色表列表", notes = "权限：view_sysRole_list")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRole_list')")
	@PostMapping("/list")
	public R<List<SysRole>> getSysRoleList(@RequestBody SysRolePO sysRolePO) {
		try{
			return R.ok(sysRoleService.getSysRoleList(sysRolePO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "通过id查询系统角色表", notes = "权限 view_sysRole_view")
	@GetMapping("/{id}" )
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRole_view')" )
	public R getById(@PathVariable("id" ) String id) {
		try{
			return R.ok(sysRoleService.getById(id));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询一条系统角色表", notes = "权限：view_sysRole_one")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRole_one')")
	@PostMapping("/one")
	public R<SysRole> getSysRole(@RequestBody SysRolePO sysRolePO) {
		try{
			return R.ok(sysRoleService.getSysRole(sysRolePO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "新增系统角色表", notes = "权限：view_sysRole_add")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRole_add')")
	@PostMapping("/add")
	public R saveSysRole(@RequestBody SysRolePO sysRolePO) {
		try{
		    return R.ok(sysRoleService.saveSysRole(sysRolePO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "修改系统角色表", notes = "权限：view_sysRole_edit")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRole_edit')")
	@PostMapping("/edit")
	public R updateSysRole(@RequestBody SysRolePO sysRolePO) {
		try{
		    return R.ok(sysRoleService.updateSysRole(sysRolePO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}    
	}

	@ApiOperation(value = "通过id删除系统角色表", notes = "权限：view_sysRole_del")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRole_del')")
	@PostMapping("/del")
	public R removeSysRole(@RequestBody SysRolePO sysRolePO) {
		try{
		    return R.ok(sysRoleService.removeSysRole(sysRolePO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}  
	}

	@ApiOperation(value = "删除角色菜单数据", notes = "权限：view_sysRole_delMenu")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRole_delMenu')")
	@PostMapping("/delMenu")
	public R delSysRoleMenu(@RequestBody SysRoleMenuPO sysRoleMenuPO) {
		try{
			return R.ok(sysRoleService.removeSysRoleMenu(sysRoleMenuPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "保存角色菜单数据", notes = "权限：view_sysRole_saveMenu")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRole_saveMenu')")
	@PostMapping("/saveMenu")
	public R saveSysRoleMenu(@RequestBody SysRoleMenuPO sysRoleMenuPO) {
		try{
			return R.ok(sysRoleService.saveSysRoleMenu(sysRoleMenuPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "获取角色的菜单列表", notes = "权限：view_sysRole_roleMenuList")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRole_roleMenuList')")
	@GetMapping("/getMenuList/{roleId}")
	public R getSysRoleMenu(@PathVariable("roleId") Integer roleId) {
		try{
			return R.ok(sysRoleService.getSysRoleMenu(roleId));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "获取角色的菜单IdList", notes = "权限：view_sysRole_roleMenuIdList")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysRole_roleMenuIdList')")
	@GetMapping("/getMenuIdList/{roleId}")
	public R getSysRoleMenuIdList(@PathVariable("roleId") Integer roleId) {
		try{
			return R.ok(sysRoleService.getSysRoleMenuId(roleId));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	
}

